package com.anxin.registerManagement.controller;

import com.alibaba.fastjson.JSONObject;
import com.anxin.registerManagement.bean.dto.RoleDto;
import com.anxin.registerManagement.bean.jo.RoleSearchJo;
import com.anxin.registerManagement.bean.persist.Role;
import com.anxin.registerManagement.service.RoleServcie;
import com.anxin.registerManagement.util.Assert;
import com.anxin.registerManagement.util.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @author 叶前呈
 * @date 2022/1/30 15:18
 */
@Api(tags = "角色接口")
@RestController
@RequestMapping("/roles")
public class RoleController {

    private static final Logger logger = LoggerFactory.getLogger(RoleController.class);

    @Autowired
    RoleServcie roleServcie;

    @ApiOperation("查询角色")
    @PostMapping("/search")
    public ResultUtil getRoleList(@RequestBody RoleSearchJo roleSearchJo) {
        logger.info("查询角色：{}", roleSearchJo);
        return ResultUtil.success(roleServcie.getRoleList(roleSearchJo));
    }

    @ApiOperation("添加角色")
    @PostMapping("/add")
    public ResultUtil addRole(@Valid @RequestBody RoleDto roleDto) {
        logger.info("添加角色：{}", roleDto);
        roleServcie.addRole(roleDto);
        return ResultUtil.success();
    }

    @ApiOperation("修改角色")
    @PostMapping("/update")
    public ResultUtil updateRole(@Valid @RequestBody RoleDto roleDto) {
        Assert.notNull(roleDto.getId(), "角色id不能为空");
        roleServcie.updateRole(roleDto);
        return ResultUtil.success();
    }

    @ApiOperation("删除角色")
    @PostMapping("/delete")
    public ResultUtil deleteRole(@RequestParam("id") List<Integer> id) {
        logger.info("删除角色：{}", id);
        Assert.notNull(id, "角色id不能为空");
        roleServcie.deleteRole(id);
        return ResultUtil.success();
    }

}
